﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Configuration;
using WebTemplate.DAL.Entities;

namespace WebTemplate.DAL.Repositories
{
    public class DoctorRepository
    {
        public void Insert(Doctor One)
        {
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = "[Doctor_Insert]";
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@MemberID", One.MemberID);
            command.Parameters.AddWithValue("@Name", One.Name);
            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();
        }

        public void Update(Doctor One)
        {
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = "[Doctor_Update]";
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@ID" ,One.ID);
            command.Parameters.AddWithValue("@MemberID", One.MemberID);
            command.Parameters.AddWithValue("@Name", One.Name);
            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();
        }

        public void Delete(int ID)
        {
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = "[Doctor_Delete]";
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@ID",ID);
            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();
        }

        public List<Doctor> Select(DoctorSearchCriteria One)
        {
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = "[Doctor_Select]";
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@ID", One.ID);
            command.Parameters.AddWithValue("@MemberID", One.MemberID);
            command.Parameters.AddWithValue("@Name", One.Name);
            connection.Open();

            SqlDataReader reader = command.ExecuteReader();
            List<Doctor> reg = new List<Doctor>();
            while (reader.Read())
            {
                Doctor obj = new Doctor();

                obj.ID = reader.GetInt32(0);
                obj.MemberID = reader.GetInt32(1);

                reg.Add(obj);
            }

            connection.Close();
            return reg;
        }

        public List<Doctor> DropDownList()
        {
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = "[DropDownListDoctors]";
            command.CommandType = System.Data.CommandType.StoredProcedure;
            connection.Open();

            SqlDataReader reader = command.ExecuteReader();
            List<Doctor> reg = new List<Doctor>();
            while (reader.Read())
            {
                Doctor obj = new Doctor();

                obj.ID = reader.GetInt32(0);
                obj.MemberID = reader.GetInt32(1);

                reg.Add(obj);
            }

            connection.Close();
            return reg;
        }

    }
}
